//
// Created by dong on 2025/10/21.
//

#include "SMenuTwo.h"

#include "Swindow.h"

SMenuTwo::SMenuTwo(Swidget* parent):SMenuTwo("menu",parent)
{

}

SMenuTwo::SMenuTwo(const string& str, Swidget* parent):Swidget(parent)
{
    SDL_Log(str.c_str());
    m_data->title=str;
    m_data->textColor={0,0,0,0};
    m_text=str;
    m_data->w = 60;
    m_data->h = 30;
    m_data->isVisible=true;
}

SActionTwo* SMenuTwo::addAction(const string& text)
{
    return SMenuTwo::addAction(new SActionTwo(text));
}

SActionTwo* SMenuTwo::addAction(SActionTwo* act)
{
    m_action.push_back(unique_ptr<SActionTwo>(act));
    return act;
}

void SMenuTwo::paintEvent(SDL_Event* msg)
{
    Swidget::paintEvent(msg);
    TTF_Font* font = TTF_OpenFont("../simkai.ttf",20);
    SDL_Surface* surface = TTF_RenderUTF8_Blended(font,m_text.c_str(),m_data->textColor);
    SDL_Texture* temp = SDL_CreateTextureFromSurface(Swindow::render,surface);
    int tw = surface->w;
    int th = surface->h;
    SDL_Rect dst = {m_data->x+(m_data->w-tw)/2,m_data->y+(m_data->h-th)/2,tw,th};
    SDL_RenderCopy(Swindow::render,temp,NULL,&dst);
    if (actionVisible==true)
    {
        paintAction(msg);
    }
    SDL_FreeSurface(surface);
    SDL_DestroyTexture(temp);
    TTF_CloseFont(font);
}

void SMenuTwo::mousePressEvent(SDL_Event* msg)
{
   if (Swidget::contains(msg->motion.x,msg->motion.y))
   {
       actionVisible = true;
   }
    if (actionVisible==true)
    {
        int mx = msg->motion.x;
        int my = msg->motion.y;
        int pos = getId(mx,my);
        if (pos >=0 &&pos <m_action.size())
        {
            m_action[pos]->pressEvent(msg);
            actionVisible=false;
        }
    }
}

void SMenuTwo::mouseMoveEvent(SDL_Event* msg)
{
    if (Swidget::contains(msg->motion.x,msg->motion.y))
    {
        m_data->isFocus=true;
    }else
    {
        m_data->isFocus=false;
    }
    int mx = msg->motion.x;
    int my = msg->motion.y;
    int pos = getId(mx,my);
    if (pos!=-1)
    {
        for (int i=0;i<m_action.size();i++)
        {
            if (i==pos)
            {
                m_action[i]->setColor({255,0,0,0});
            }else
            {
                m_action[i]->setColor({238,238,242,0});
            }
        }
    }
}

void SMenuTwo::paintAction(SDL_Event* mgs)
{
    TTF_Font* font = TTF_OpenFont("../simkai.ttf",20);
    SDL_Color black = {0,0,0,0};
    for (int i=0;i<m_action.size();i++)
    {
        SDL_Surface* surface = TTF_RenderUTF8_Blended(font,m_action[i]->text().c_str(),black);
        SDL_Texture* temp = SDL_CreateTextureFromSurface(Swindow::render,surface);
        int tw = surface->w;
        int th = surface->h;
        int bx = m_data->x;
        int by = m_data->y + m_data->h * (i+1);
        SDL_Rect dst = {bx+(200-tw)/2,by+(30-th)/2,tw,th};
        SDL_Color color = m_action[i]->getColor();
        SDL_SetRenderDrawColor(Swindow::render,color.r,color.g,color.b,0);
        SDL_Rect rect={bx,by,200,30};
        SDL_RenderFillRect(Swindow::render,&rect);
        SDL_RenderCopy(Swindow::render,temp,nullptr,&dst);
        SDL_FreeSurface(surface);
        SDL_DestroyTexture(temp);
    }

    TTF_CloseFont(font);
}

int SMenuTwo::getId(int x, int y)
{

    if (x<m_data->x || x > m_data->x+200)
    {
        return  -1;
    }
    if (y-m_data->y-m_data->h < 0)
    {
        return -1;
    }

    return (y-m_data->y-m_data->h)/30;

}
